package com.zofer.study;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class FiveThreadPoolTest {

//    static ExecutorService poolService = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("zofer-pool").build());
//    static ExecutorService poolService = Executors.newFixedThreadPool(2,new ThreadFactoryBuilder().setNameFormat("zofer-pool").build());
//    static ExecutorService poolService = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("zofer-pool").build());
//    static ScheduledExecutorService poolService = Executors.newScheduledThreadPool(3,new ThreadFactoryBuilder().setNameFormat("zofer-pool").build());
    static ExecutorService poolService = Executors.newWorkStealingPool(4);

    static class Task implements Runnable{
        int count;
        public Task(int count) {
            this.count = count;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName()+"正在执行...");
//                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName()+"执行完了");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args){
        for (int i = 0; i < 10 ; i++) {
            Task t1 = new Task(i);
            poolService.execute(t1);
//            poolService.scheduleWithFixedDelay(t1,2,3, TimeUnit.SECONDS);
        }
        System.out.println("主线程:"+Thread.currentThread().getName());
//        poolService.shutdown();
    }
}
